<?php

/**
 * 推广统计
 * File Name：SpreadController.php
 * File Encoding：UTF-8
 * File New Time：2014-7-24 11:06:06
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class SpreadController extends AdminBase {

    public function actionIndex() {
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('index', $_POST));
            exit;
        }
        $where = array(
            'order' => 'id desc',
            'goods_bm_status' => 1,
        );

        $keyword = $_GET['keyword'];
        if (!empty($keyword)) {
            $type = (int) $_GET['type'];
            switch ($type) {
                case 1:
                    $where['GoodsBmModel.product_id'] = $keyword;
                    break;
                case 2:
                    $where['GoodsBmModel.goods_name'] = array('LIKE', "%{$keyword}%");
                    break;
                default:
                    $where['GoodsBmModel.nick'] = $keyword;
                    break;
            }
            $this->assign('keyword', $keyword);
            $this->assign('type', $type);
        }
        //推广人
        $recommend = (int) $_GET['recommend'];
        if ($recommend) {
            $where['userid'] = $recommend;
            $this->assign('recommend', $recommend);
        }
        //报名时间
        $start_time = $_GET['start_time'];
        if ($start_time) {
            $start_time = strtotime($_GET['start_time']);
            if ($_GET['end_time']) {
                $end_time = strtotime($_GET['end_time']) + 86399;
                $this->assign('end_time', $_GET['end_time']);
            }
            if ($start_time) {
                $where['status_time'] = array('EGT', $start_time);
            }
            if ($end_time && $end_time > $start_time) {
                $where['status_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'and');
            } else if ($end_time && $end_time <= $start_time) {
                $where['status_time'] = array(array('EGT', $start_time), array('ELT', $start_time + 86399), 'and');
            }
            $this->assign('start_time', $_GET['start_time']);
        }

        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        $model = GoodsSpreadModel::model();
        $criteria = $model->where($where);
        //信息总数
        $count = $model->with('GoodsBmModel')->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->with('GoodsBmModel')->findAll($criteria);
        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->render();
    }

    //数据统计分析
    public function actionAnalysis() {
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('analysis', $_POST));
            exit;
        }
        //条件
        $where = array();
        $recommend = (int) $_GET['recommend'];
        if ($recommend) {
            $this->assign('recommend', $recommend);
            $where['userid'] = $recommend;
        }
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        $model = GoodsSpreadModel::model();
        $criteria = $model->where($where);
        $criteria->group = 'userid';
        //总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);
        foreach ($data as $k => $rs) {
            $rs = $rs->attributes;
            $exp = array(
                'userid' => $rs['userid'],
            );
            //报名时间
            $start_time = $_GET['start_time'];
            if ($start_time) {
                $start_time = strtotime($_GET['start_time']);
                if ($_GET['end_time']) {
                    $end_time = strtotime($_GET['end_time']) + 86399;
                    $this->assign('end_time', $_GET['end_time']);
                }
                if ($start_time) {
                    $exp['create_time'] = array('EGT', $start_time);
                }
                if ($end_time && $end_time > $start_time) {
                    $exp['create_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'and');
                } else if ($end_time && $end_time <= $start_time) {
                    $exp['create_time'] = array(array('EGT', $start_time), array('ELT', $start_time + 86399), 'and');
                }
                $this->assign('start_time', $_GET['start_time']);
            }
            //总数
            $rs['count'] = $model->count($model->where($exp));
            $expCri = $model->where($exp);
            $expCri->group = 'goods_bm_status';
            $expCri->select = 'goods_bm_status,count(goods_bm_status) as create_time';
            $statusList = $model->findAll($expCri);
            foreach ($statusList as $i => $st) {
                unset($statusList[$i]);
                $statusList[$st->goods_bm_status] = $st['create_time'];
            }
            $rs['status'] = $statusList;
            $data[$k] = $rs;
        }
        $this->assign("Page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->render();
    }

    //更改推荐人信息
    public function actionSaveuser() {
        $id = (int) $_GET['id'];
        $recommend = (int) $_GET['recommend'];
        if (empty($id) || empty($recommend)) {
            $this->error('该信息不存在！');
        }
        $model = GoodsSpreadModel::model();
        $data = $model->findByPk($id);
        if (empty($data)) {
            $this->error('该信息不存在！');
        }
        if ($model->saveSpreadUser($data->goods_bm_id, $recommend)) {
            $this->success('修改成功！');
        } else {
            $this->error('修改失败！');
        }
    }

    //删除推广记录
    public function actionDelete() {
        $this->baseDelete(GoodsSpreadModel::model(), 'index');
    }

}
